package com.lagou.controller;

import com.github.pagehelper.PageInfo;
import com.lagou.domain.PromotionAd;
import com.lagou.domain.PromotionAdVO;
import com.lagou.domain.ResponseResult;
import com.lagou.service.PromotionAdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 李树飞
 */
@RestController
@RequestMapping("/PromotionAd")
public class PromotionAdController {

    @Autowired
    private PromotionAdService promotionAdService;

    /**
     * 广告分页
     * @param promotionAdVO
     * @return
     */
    @RequestMapping("/findAllPromotionAdByPage")
    public ResponseResult findAllAdByPage(PromotionAdVO promotionAdVO) {
        PageInfo<PromotionAd> allPromotionAdByPage = promotionAdService.findAllAdByPage(promotionAdVO);
        return new ResponseResult(true,200,"广告查询成功",allPromotionAdByPage);
    }

    /**
     * 图片上传和回显
     * @param file
     * @param request
     * @return
     * @throws IOException
     */
    @RequestMapping("/PromotionAdUpload")
    public ResponseResult fileUpload(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws IOException {
        //判断文件是否为控
        if (file.isEmpty()) {
            throw new RuntimeException();
        }

        ////获取文件真实路径
        String realPath = request.getServletContext().getRealPath("/");

        //截取文件路径
        String newRealPath = realPath.substring(0, realPath.indexOf("ssm_web"));

        //获取文件名
        String fileName = file.getOriginalFilename();

        //拼接新的文件名
        String newFileName = System.currentTimeMillis() + fileName.substring(fileName.lastIndexOf("."));

        //创建新的文件并上传
        String upLoadPath = newRealPath + "upload\\";
        File newFile = new File(upLoadPath, newFileName);

        //判断目录是否存在
        if (!newFile.getParentFile().exists()) {
            newFile.getParentFile().mkdirs();
            System.out.println("创建目录" + newFile);
        }
        file.transferTo(newFile);

        Map<String, String> map = new HashMap<>();
        map.put("fileName", newFileName);
        map.put("filePath", "http://localhost:8080/upload/" + newFileName);

        return new ResponseResult(true, 200, "上传成功", map);
    }

    /**
     * 广告动态上下线
     * @param id
     * @param status
     * @return
     */
    @RequestMapping("/updatePromotionAdStatus")
    public ResponseResult updatePromotionAdStatus(int id, int status) {
        promotionAdService.updatePromotionAdStatus(id,status);
        return new ResponseResult(true,200,"修改成功",null);
    }

    /**
     * 回显广告信息
     * @param id
     * @return
     */
    @RequestMapping("/findPromotionAdById")
    public  ResponseResult findPromotionAdById(@RequestParam int id){
        PromotionAd promotionAd = promotionAdService.findPromotionAdById(id);
        return new ResponseResult(true,200,"回显成功",promotionAd);
    }
}
